package com.glsc.ngateway.platform.service.itflow.config;

import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.flow.FlowConfig;
import com.glsc.ngateway.common.api.common.dto.PageQueryParamDto;
import com.glsc.ngateway.common.api.flowable.dto.task.config.FlowConfigDto;
import com.glsc.ngateway.common.api.flowable.dto.task.config.FlowConfigSearchDto;
import com.glsc.ngateway.platform.vo.FlowTaskParamPreVo;
import com.glsc.ngateway.common.base.converter.platform.FlowConfigConverter;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.FlowConfigRepo;
import com.glsc.ngateway.platform.service.itflow.ItFlowService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * 流程配置
 *
 * @author xuchang
 */
@Service
public class ItFlowConfigService {

    Logger logger = LoggerFactory.getLogger(ItFlowConfigService.class);

    @Autowired
    private FlowConfigRepo configRepo;

    @Autowired
    private ItFlowService itFlowService;

    @Autowired
    private ProcessEditConfigService processEditConfigService;

    /**
     * 分页查询
     */
    public Page<FlowConfigDto> page(PageQueryParamDto<FlowConfigSearchDto> param) {
        FlowConfigSearchDto paramDto = Objects.nonNull(param.getParam()) ? param.getParam() : new FlowConfigSearchDto();
        Pageable pageable = param.getPageable();
        Page<FlowConfig> pageParam = configRepo.pageParam(pageable, paramDto);
        List<FlowConfigDto> dtoList = FlowConfigConverter.do2Dto(pageParam.getContent());
        //填充编辑字段配置
        dtoList.forEach(c -> {
            c.setEditConfigDto(processEditConfigService.getProcessEditConfigDto(c.getProcessDefKey(), c.getTaskDefKey()));
        });
        return new PageImpl<>(dtoList, pageable, pageParam.getTotalElements());
    }

    public List<FlowConfigDto> query(FlowConfigSearchDto param) {
        //预处理搜索条件
        List<FlowConfig> res = configRepo.query(param);
        return FlowConfigConverter.do2Dto(res);
    }

    /**
     * 删除
     */
    public void delete(@NotNull Long id) {
        //FlowConfig config = configRepo.findById(id).orElseThrow(() -> PlatformException.error("未找到配置信息"));
        configRepo.deleteById(id);
        //轮询删除表单可编辑配置
//        try {
//            processEditConfigService.updateDeletedByProcessTypeAndNodeType(config.getProcessDefKey(), config.getTaskDefKey());
//        } catch (Exception e) {
//            logger.error("流程配置删除：删除失败，" + e.getMessage());
//        }
    }

    /**
     * 添加或修改
     */
    public void addOrUpdate(@NotNull FlowConfigDto flowConfig) {
//        String processDefKey = flowConfig.getProcessDefKey();
//        String processDefName = ITFlowEnum.seek(processDefKey).getDesc();
        FlowConfig config = FlowConfigConverter.dto2Do(flowConfig);
        configRepo.save(config);
        //这里只修改流程人员变量相关配置，节点可编辑配置在其他入口修改
        return;
        //流程节点表单可编辑配置，Vo中不是空对象，前端也不传空对象
//        ProcessEditConfigDto editConfigDto = Objects.nonNull(flowConfig.getEditConfigDto()) ? flowConfig.getEditConfigDto() : new ProcessEditConfigDto();
//        editConfigDto.setProcessType(processDefKey);
//        editConfigDto.setTaskDescription(processDefName);
//        editConfigDto.setNodeType(flowConfig.getTaskDefKey());
//        editConfigDto.setNodeName(ITFlowTaskEnum.seek(flowConfig.getTaskDefKey()).getDesc());
//        editConfigDto.setActivated(DictConstant.ACTIVATED_YES);
//        ProcessEditConfig processEditConfig = ProcessEditConfigMapper.INSTANCE.dto2domain(editConfigDto);
//        processEditConfigService.saveOrUpdate(processEditConfig);

    }

    /**
     * 预览创建
     */
    public FlowTaskParamPreVo preViewCreateFlow(String flowKey, String user) {
        return itFlowService.preViewCreateFlow(flowKey, user);
    }
}
